class Compiler{
    constructor(vm){
        this.vm=vm
        this.el=vm.$el
        this.compile(this.el)
    }

    compile(el){
        let childNodes=el.childNodes
        Array.from(childNodes).forEach(node=>{
            // console.dir(node)
            if(this.isText(node)){
                // 文本节点
                
                this.compileText(node)
            }else if(this.isElement(node)){
                this.compileElement(node)
                // 元素节点
            }

            if(node.childNodes && node.childNodes.length){
                this.compile(node)
            }
        })
    }
    compileText(node){
        let reg=/\{\{(.+?)\}\}/
        let value=node.textContent
        
        // console.log(this.vm)
        
        if(reg.test(value)){
            let key=RegExp.$1.trim()
            node.textContent=value.replace(reg,this.vm[key])
        }
    }

    compileElement(node){
        console.log(node.attributes)
        Array.from(node.attributes).forEach(item=>{
            let attrName=item.name
            if(this.isString(attrName)){
                node.textContent=this.vm[item.value]
                node.value=this.vm[item.value]
            }
            
        })
    }
    isString(attrName){
        return attrName.startsWith('v-')
    }

    isElement(node){
        return node.nodeType===1
    }
    isText(node){
        return node.nodeType===3
    }
}